// 预加载库
// let assert = require('assert');
let chai = require('chai');
let request = require('supertest');
let app = "http://3dmomoda.com";
let nightwatch = require('nightwatch');
let config = require('../nightwatch.json');
const cv = require('opencv');
/*
详细使用介绍
https://www.jianshu.com/p/f200a75a15d2
断言语言链
to be been is that which and has have with at of same
*/
let expect = chai.expect;

// 预加载函数
function getHistogram(mat) {
  let arr = [];
  for (var i = 0; i < 64; i++) {
      arr[i] = 0;
  }
  let pow4 = Math.pow(4, 2);
  let data = mat.toBuffer(); 
  for (var i = 0, len = data.length; i < len; i += 4) {
      var red = (data[i] / 64) | 0;
      var green = (data[i + 1] / 64) | 0;
      var blue = (data[i + 2] / 64) | 0;
      var index = red * pow4 + green * 4 + blue;
      arr[index]++;
  }

  return arr;
}

function cosine(arr1, arr2) {
  var axb = 0,
      a = 0,
      b = 0;
  for (var i = 0, len = arr1.length; i < len; i++) {
      axb += arr1[i] * arr2[i];
      a += arr1[i] * arr1[i];
      b += arr2[i] * arr2[i];
  }
  return axb / (Math.sqrt(a) * Math.sqrt(b));
} 

// 初始化参数
let client = nightwatch.initClient(config.test_settings.default);
let browser = client.api();

// 待测试函数
function sum(a, b){
  return a + b;
}

// 逻辑结构测试
describe('逻辑结构测试',function(){
  this.timeout(2000); // 默认值
  describe('测试函数sum',function(){
    it('当传入1，2的时候返回值应该是3',function(){
      let s = sum(1, 2);
      expect(s).to.be.equal(3);
    });
  });
});

// 逻辑结构测试
describe('数据结构测试',function(){
  this.timeout(2000); // 默认值
  describe('测试post请求/login',function(){
    it('返回数据中必须有state',function(){
      request(app)
        .post('/restGallery/query?pageSize=24&pageNumber=3&sort=zonghe&date=%E6%89%80%E6%9C%89%E6%97%B6%E9%97%B4&keyword=&stype%5BUDCB%5D=false&stype%5BMMD%5D=false&format%5BWebGL%5D=false&format%5BU3D%5D=false')
        .end(function(err, res) { 
          expect(res.body).to.have.any.keys('state');
        });
    });
  });
});

// 被测试图片
let tempPath = './screenshots/b.png';
// 测试原图 正常情况下需要用nightwatch截图
// 截图直接使用 .saveScreenshot('./screenshots/fileName.png') 即可
let sourcePath = './screenshots/a.png';
// 界面测试
describe('界面测试', function() {
  this.timeout(20000);
  describe('测试图片匹配程度', function() {
    // 用图片直方图对比余弦相似度
    it('在原图中找到图片内容并对比相似度应该在98%以上', function(done) {
      cv.readImage(sourcePath, (err, sourceImg) => {
        if (err) return done(err);
        let result = sourceImg.matchTemplate(tempPath, 5);
        let newMat = sourceImg.crop(result[1], result[2], result[3], result[4]);
        cv.readImage(tempPath, (error, tempImg) => {
          if (error) return done(err);
          let per = cosine(getHistogram(newMat),getHistogram(tempImg));
          expect(per >= 0.98).to.be.ok;
          done();
        });
      });
    });
  });

  describe('界面元素测试', function() {
    it('必须有预定义的元素', function(done) {
      browser
        .maximizeWindow()
        .url('https://www.baidu.com')
        .waitForElementVisible('body', 3000)
        .setValue('#kw', '衷于栖')
        .click('#su')
        .pause(3000)
        .assert.containsText('#wrapper_wrapper', '衷于栖') 
        .saveScreenshot('./screenshots/fileName.png')
        .end();

      client.start(done);
    });
  });
});





